package com.ccsu.joysport.controller;

import com.ccsu.joysport.controller.model.resp.ClubPageResp;
import com.ccsu.joysport.dto.*;
import com.ccsu.joysport.entity.Activity;
import com.ccsu.joysport.entity.Club;
import com.ccsu.joysport.service.ClubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ccsu.joysport.util.JwtUtil;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("/ccsu/c/student/club")
public class ClubController {

    @Autowired
    private ClubService clubService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 俱乐部列表 - 分页
     */
    @GetMapping("/page")
    public ClubPageResp clubPage(@RequestParam String token,
                                 @RequestParam(required = false) String searchText,
                                 @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {
        if (!jwtUtil.validateToken(token)) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "无效的token");
        }

        return clubService.clubPage(searchText, pageNum - 1, pageSize);
    }

    //查询单个俱乐部详情
    @GetMapping("/queryClubDetail")
    public Club queryClubDetail(@RequestParam String token, @RequestParam String clubId) {
        if (!jwtUtil.validateToken(token)) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "无效的token");
        }

        return clubService.getClubDetail(clubId);
    }

    //申请加入俱乐部
    @PostMapping("/applyJoinClub")
    public ResponseEntity<?> applyJoinClub(@RequestBody ApplyJoinClubRequest request) {
        if (!jwtUtil.validateToken(request.getToken())) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        ApplyJoinClubResponse response = clubService.applyJoinClub(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //查看俱乐部开展的活动
    @GetMapping("/viewClubActivities")
    public ResponseEntity<?> viewClubActivities(@RequestParam String token, @RequestParam String clubId) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        List<ClubActivityResponse> activities = clubService.getClubActivities(clubId);
        return new ResponseEntity<>(activities, HttpStatus.OK);
    }

    //查看学生已加入的俱乐部
    @GetMapping("/viewJoinedClubs")
    public ResponseEntity<?> viewJoinedClubs(@RequestParam String token) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        List<Club> clubs = clubService.getJoinedClubs(token);
        return new ResponseEntity<>(clubs, HttpStatus.OK);

    }
    //查询俱乐部
    @GetMapping("/searchClub")
    public ResponseEntity<?> searchClub(@RequestParam String token, @RequestParam String queryText) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        List<Club> clubs = clubService.searchClub(queryText);
        return new ResponseEntity<>(clubs,HttpStatus.OK);
    }

    //查询俱乐部申请状态
    @GetMapping("/queryApplicationStatus")
    public ResponseEntity<?> queryApplicationStatus(@RequestParam String token) {
        if (!jwtUtil.validateToken(token)) {
            return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
        }

        List<ClubApplicationResponse> applications = clubService.getApplicationsByStudentId(token);
        return new ResponseEntity<>(applications, HttpStatus.OK);
    }

 //创建俱乐部
 @PostMapping("/createClub")
 public ResponseEntity<?> createClubInfo(@RequestBody CreateClubRequest request) {
     if (!jwtUtil.validateToken(request.getToken())) {
         return new ResponseEntity<>("无效的token", HttpStatus.UNAUTHORIZED);
     }
     ClubResponse response = clubService.createClub(request);
      return new ResponseEntity<>(response, HttpStatus.OK);
 }

//查询创建的俱乐部申请状态
@GetMapping("/getCreatedClubs")
public ResponseEntity<?> getCreatedClubs(@RequestParam String token) {
    String studentId;
    try {
        studentId = jwtUtil.extractId(token);
    } catch (Exception e) {
        return new ResponseEntity<>("token解析失败", HttpStatus.UNAUTHORIZED);
    }

    List<CreateClubResponse> createdClubs = clubService.getCreatedClubs(studentId);
    return new ResponseEntity<>(createdClubs, HttpStatus.OK);
    }
}

